﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    Datacon dataconn = new Datacon();
    private void getcom()
    {
        Datacon dn = new Datacon();
        DataSet ds = dn.GetDataSetSql(string.Format(@"select dbo.[USER].ID,
        dbo.[USER].TYPE,
        dbo.[USER].LOGIN_NAME,
        dbo.[USER].LOGIN_PASS,
        dbo.[USER].USER_NAME,
        dbo.[USER].COMPANY,
        dbo.[USER].DEPARTMENT
        from[USER] where LOGIN_NAME='{0}' AND LOGIN_PASS='{1}'", TextBox5.Text, TextBox6.Text));
        DataTable dt = ds.Tables[0];
        if (dt.Rows.Count <= 0)
        {
            Response.Write("<script language=javascript>alert('用户名或密码错误！')</script>");
            Response.Write("<script language=javascript>window.location.href='Index.aspx'</script>");
        }
        else
        {

            foreach (DataRow dr in dt.Rows)
            {
                Session["ID"] = dr["ID"].ToString();
                Session["PWD"] = TextBox6.Text;
                Session["TYPE"] = dr["type"].ToString();
                Session["NAME"] = dr["USER_NAME"].ToString();

                if (dr["type"].ToString() == "CUS")
                {

                    Response.Redirect("User/or.aspx");
                }
                else if (dr["type"].ToString() == "ADMIN")
                {

                    Page.Response.Redirect("Admin/admin.aspx");
                }
                else

               if (dr["type"].ToString() == "EMP")
                {

                    Response.Redirect("Teacher/teacherLayui.aspx");
                }
                else
                {
                    Response.Write("<script language=javascript>alert('用户名或密码错误！')</script>");
                    Response.Write("<script language=javascript>window.location.href='Index.aspx'</script>");

                }
            }


        }
    }

    public static string EncryptWithMD5(string source)
    {
        byte[] sor = Encoding.UTF8.GetBytes(source);
        MD5 md5 = MD5.Create();
        byte[] result = md5.ComputeHash(sor);
        StringBuilder strbul = new StringBuilder(40);
        for (int i = 0; i < result.Length; i++)
        {
            strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位

        }
        return strbul.ToString();
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        HttpCookieCollection checkCode = Request.Cookies;
        // string a = TextBox7.Text;
        //  string b = checkCode["CheckCode"].Value;
        if (TextBox5.Text == "" || TextBox6.Text == "")
        {
            Response.Write("<script>alert('请输入完整！')</script>");
            Response.Write("<script language=javascript>window.location.href='Index.aspx'</script>");

        }
        //else if (!a.Equals(b, StringComparison.OrdinalIgnoreCase))
        //{
        //    Response.Write("<script>alert('验证码有误！')</script>");
        //    Response.Write("<script language=javascript>window.location.href='Index.aspx'</script>");

        //}
        else
        {
            getcom();

        }


    }

}